package com.tisson.action;

import java.text.Bidi;
import java.util.ArrayList;
import java.util.List;

import com.sun.accessibility.internal.resources.accessibility;
import com.tisson.bo.AreaBO;
import com.tisson.bo.DepartmentBO;
import com.tisson.bo.DoorBO;
import com.tisson.bo.PurviewBO;
import com.tisson.comment.WebConstants;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.AcDoor;
import com.tisson.pojo.Area;
import com.tisson.pojo.Page;

public class DoorAction extends ActionSup{
	

	private List result;
	private Page page;
	private String id;
	private List areaList;
	private List districtList;
	private List buildingList;
	private List doorList;
	private String districtId;
	private String areaId;
	private String buildingId;
	private String doorId;
	private String doorAddress;
	private String doorBuilding;
	private String doorChn;
	private String doorDelay;
	private String doorName;
	private String doorSerial;
	private String doorSystem;
	private String doorTic;
	private String aid;
	private String did;
	private String bid;
	
	public String search(){
		DepartmentBO bo = new DepartmentBO();
		page = new Page();
		List all = new ArrayList();
		result = new ArrayList();
		try{
			/*all = bo.search(departmentId, depName, depAdmin, depAddress, depPhone, logicMark0, logicMark1, logicMark2);*/
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "list";
	}
	
	
	public String delete(){
	    DoorBO bo = new DoorBO();
		String[] idList = request.getParameterValues("Id");
		if(idList!=null){
			for(int i=0;i<idList.length;i++){
				AcDoor acDoor = new AcDoor();
				acDoor.setDoorId(idList[i]);
				bo.delete(acDoor);
			}
		}
		return doorList();
	}
	
	public String add(){
		DoorBO bo = new DoorBO();
		AcDoor acDoor = new AcDoor();
		acDoor.setDoorAddress(doorAddress);
		acDoor.setDoorBuilding(bid);
		acDoor.setDoorChn(Integer.parseInt(doorChn));
		acDoor.setDoorDelay(Integer.parseInt(doorDelay));
		acDoor.setDoorName(doorName);
		acDoor.setDoorSerial(doorSerial);
		acDoor.setDoorSystem(Integer.parseInt(doorSystem));
		acDoor.setDoorTic(Integer.parseInt(doorTic));
		bo.add(acDoor);
		return doorList();
	}
	
	public String edit(){
		try{
			DoorBO bo = new DoorBO();
			AcDoor acDoor = bo.findById(id);
			doorAddress = acDoor.getDoorAddress();
			buildingId = acDoor.getDoorBuilding();
			doorChn = String.valueOf(acDoor.getDoorChn());
			doorDelay = String.valueOf(acDoor.getDoorDelay());
			doorName = acDoor.getDoorName();
			doorSerial = acDoor.getDoorSerial();
			doorSystem = String.valueOf(acDoor.getDoorSystem());
			doorTic = String.valueOf(acDoor.getDoorTic());
			doorId =id;
			did =  districtId;
			aid = areaId;
			bid = acDoor.getDoorBuilding();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return "editView";
	}
	
	public String update(){
		try{
			DoorBO bo = new DoorBO();
			AcDoor acDoor = bo.findById(doorId);
			//acDoor.setDoorId(doorId);
			acDoor.setDoorBuilding(bid);
			acDoor.setDoorAddress(doorAddress);
			acDoor.setDoorChn(Integer.parseInt(doorChn));
			acDoor.setDoorDelay(Integer.parseInt(doorDelay));
			acDoor.setDoorName(doorName);
			acDoor.setDoorSerial(doorSerial);
			acDoor.setDoorSystem(Integer.parseInt(doorSystem));
			acDoor.setDoorTic(Integer.parseInt(doorTic));
			bo.update(acDoor);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return doorList();
	}
	
	

	public String doorList(){
		PurviewBO bo =  new PurviewBO();
		page = new Page();
		List all;
		setAreaId(aid);
		setDistrictId(did);
		setBuildingId(bid);
		result = new ArrayList();
		areaList = bo.getOptionArea(request);
		districtList = bo.getOptionDistrict(aid);
		buildingList = bo.getOptionBuilding(did);
		doorList = bo.getOptionDoor(bid);
		try{
			all = bo.getOptionDoor(bid);
		}
		catch(Exception e){
			e.printStackTrace();
			all = new ArrayList();
		}
		request.getSession().setAttribute("list", all);
		try{
			for(int i=0;i<WebConstants.PAGESIZE;i++){
				if(all.get(i)!=null){
					result.add(all.get(i));
				}
			}
		}catch(Exception e){
		}
		page.setCurrentPage(1);
		page.setPrePage(page.getCurrentPage()-1);
		page.setNextPage(page.getCurrentPage()+1);
		page.setListSize(all.size());
		page.setTotalPages(page.getListSize()%WebConstants.PAGESIZE==0?page.getListSize()/WebConstants.PAGESIZE:(page.getListSize()/WebConstants.PAGESIZE)+1);
		return "doorList";
	}
	
	
	public List getResult() {
		return result;
	}
	public void setResult(List result) {
		this.result = result;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	public List getDistrictList() {
		return districtList;
	}

	public void setDistrictList(List districtList) {
		this.districtList = districtList;
	}

	public List getBuildingList() {
		return buildingList;
	}

	public void setBuildingList(List buildingList) {
		this.buildingList = buildingList;
	}

	public List getDoorList() {
		return doorList;
	}

	public void setDoorList(List doorList) {
		this.doorList = doorList;
	}

	public String getDistrictId() {
		return districtId;
	}

	public void setDistrictId(String districtId) {
		this.districtId = districtId;
	}

	public String getBuildingId() {
		return buildingId;
	}

	public void setBuildingId(String buildingId) {
		this.buildingId = buildingId;
	}

	public String getDoorId() {
		return doorId;
	}

	public void setDoorId(String doorId) {
		this.doorId = doorId;
	}

	public String getAreaId() {
		return areaId;
	}

	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}


	public String getDoorAddress() {
		return doorAddress;
	}


	public void setDoorAddress(String doorAddress) {
		this.doorAddress = doorAddress;
	}


	public String getDoorBuilding() {
		return doorBuilding;
	}


	public void setDoorBuilding(String doorBuilding) {
		this.doorBuilding = doorBuilding;
	}


	public String getDoorChn() {
		return doorChn;
	}


	public void setDoorChn(String doorChn) {
		this.doorChn = doorChn;
	}


	public String getDoorDelay() {
		return doorDelay;
	}


	public void setDoorDelay(String doorDelay) {
		this.doorDelay = doorDelay;
	}


	public String getDoorName() {
		return doorName;
	}


	public void setDoorName(String doorName) {
		this.doorName = doorName;
	}


	public String getDoorSerial() {
		return doorSerial;
	}


	public void setDoorSerial(String doorSerial) {
		this.doorSerial = doorSerial;
	}


	public String getDoorSystem() {
		return doorSystem;
	}


	public void setDoorSystem(String doorSystem) {
		this.doorSystem = doorSystem;
	}


	public String getDoorTic() {
		return doorTic;
	}


	public void setDoorTic(String doorTic) {
		this.doorTic = doorTic;
	}


	public String getAid() {
		return aid;
	}


	public void setAid(String aid) {
		this.aid = aid;
	}


	public String getDid() {
		return did;
	}


	public void setDid(String did) {
		this.did = did;
	}


	public String getBid() {
		return bid;
	}


	public void setBid(String bid) {
		this.bid = bid;
	}

	

}
